import { FModule } from "../../Libs/Framework/Support/FModule";
import { EventManager } from "../../Libs/Framework/Managers/EventManager";
import { ModuleEvent } from "../../Libs/Framework/Event/ModuleEvent";
import { ModuleNames } from "../../Libs/Framework/Enums/ModuleNames";
import { WriteCommand } from "../../Libs/NetWork/WriteCommand/WriteCommand";
import { WriteCmdEvent } from "../../Libs/Framework/Event/WriteCmdEvent";

export default class joinDeskModule extends FModule {
    public get assets(): any { return ["Lobby/joinDesk"] }

    private _deskno: cc.Node
    private _buttonGroup: cc.Node
    private _desknoArray : string[] = null
    private _close : cc.Node = null

    // protected initViews() : void {
    //     super.initViews()

    // }

    protected createViews(): void {
        this._desknoArray = []
        this._deskno = this.node.getChildByName("DeskNo")
        this._buttonGroup = this.node.getChildByName("ButtonGroup")
        this._close = this.node.getChildByName("close")
        this._close.on(cc.Node.EventType.TOUCH_END, this.close, this)
        this.addClickEvent(true)
        this.clearDeskno()
        
    }

    private clearDeskno(): void {
        for (let i = 0; i < this._deskno.childrenCount; i++) {
            this._deskno.children[i].getComponent(cc.Label).string = ""
        }
    }

    private addClickEvent(canOn: boolean = false): void {
        for (let i = 0; i < this._buttonGroup.childrenCount; i++) {
            if (canOn)
                this._buttonGroup.children[i].on(cc.Node.EventType.TOUCH_END, this.touchFunc, this)
            else
                this._buttonGroup.children[i].off(cc.Node.EventType.TOUCH_END, this.touchFunc)

        }
    }

    private close() : void {
        EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.Joindesk);
        EventManager.dispatchModuleEventWith(ModuleEvent.DISPOSE_MODULE, ModuleNames.Mask)
    }

    private touchFunc(evt: cc.Event.EventTouch): void {
        switch (evt.target.name) {
            case "clear": case "del":
                if(evt.target.name == "del")this.delDeskNum()
                else this.clearDeskNum()
                break;
            default:
                this.addDeskNum(evt.target.name)
        }
    }

    private delDeskNum () : void {
        if(this._desknoArray.length <= 0)return
        let i = this._desknoArray.length - 1
        this._desknoArray.splice(i,1)
        this.refreshDesk()
    }

    private clearDeskNum ():void {
        this._desknoArray = []
        this.refreshDesk()
    }

    private addDeskNum (s:string) : void {
        if(this._desknoArray.length >= 6)return
        this._desknoArray.push(s)
        this.refreshDesk()
        if(this._desknoArray.length == 6){
            let desk = this._desknoArray.join("")
            new WriteCommand(WriteCmdEvent.CMD_JoinDesk,desk)
            this.clearDeskNum()
            this.close()
        }
        
    }

    public dispose(): void {
        //this.addClickEvent(true)
        //this._close.off(cc.Node.EventType.TOUCH_END, this.close)
        super.dispose();
    }

    private refreshDesk () : void {
        this.clearDeskno()
        for(let i = 0 ; i < this._desknoArray.length;i++){
            this._deskno.children[i].getComponent(cc.Label).string = this._desknoArray[i]
        }
    }



    
}



